package nmf;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Random;

import org.ejml.ops.RandomMatrices;
import org.ejml.simple.SimpleMatrix;

public class MatrixFactory {
   
   public static void main(final String args[]) throws Exception {
      final PrintWriter out =
            new PrintWriter(new FileWriter(
                  "C:\\Users\\Garrett\\Documents\\NMFMatrices\\Matrices.txt"));
      SimpleMatrix V, W, H;
      out.println("Small matrix: \n");
      
      W = new SimpleMatrix(new double[2][1]);
      RandomMatrices.setRandom(W.getMatrix(), 0, 10, new Random());
      
      H = new SimpleMatrix(new double[1][2]);
      RandomMatrices.setRandom(H.getMatrix(), 0, 10, new Random());
      V = W.mult(H);
      
      out.print("\r\n W =");
      PrintMatrix(out, W, 2, 1);
      out.print("\r\n\r\n");
      
      out.print("\r\n H =");
      PrintMatrix(out, H, 1, 2);
      out.print("\r\n\r\n");
      
      out.print("\r\n V =");
      PrintMatrix(out, V, 2, 2);
      
      out.print("\r\n\r\n");
      out.print("Medium Matrix: \r\n");
      
      W = new SimpleMatrix(new double[8][3]);
      RandomMatrices.setRandom(W.getMatrix(), 0, 10, new Random());
      H = new SimpleMatrix(new double[3][7]);
      RandomMatrices.setRandom(H.getMatrix(), 0, 10, new Random());
      V = W.mult(H);
      
      out.print("\r\n W =");
      PrintMatrix(out, W, 8, 3);
      out.print("\r\n\r\n");
      out.print("\r\n H =");
      PrintMatrix(out, H, 3, 7);
      
      out.print("\r\n\r\n");
      out.print("\r\n V =");
      PrintMatrix(out, V, 8, 7);
      
      out.print("\r\n\r\n");
      out.print("Large Matrix: \r\n");
      W = new SimpleMatrix(new double[100][10]);
      RandomMatrices.setRandom(W.getMatrix(), 0, 10, new Random());
      H = new SimpleMatrix(new double[10][24]);
      RandomMatrices.setRandom(H.getMatrix(), 0, 10, new Random());
      V = W.mult(H);
      
      out.print("\r\n W =");
      PrintMatrix(out, W, 100, 10);
      out.print("\r\n\r\n");
      out.print("\r\n H =");
      PrintMatrix(out, H, 10, 24);
      
      out.print("\r\n\r\n");
      out.print("\r\n V =");
      PrintMatrix(out, V, 100, 24);
      
      out.close();
      
   }
   
   public static void PrintMatrix(final PrintWriter out, final SimpleMatrix V, final int n, final int m) {
      
      for (int i = 0; i < n; i++) {
         out.print("\r\n");
         if (i == 0) {
            out.print(" {{");
         }
         else if (i != 0) {
            out.print("  {");
         }
         
         for (int j = 0; j < m; j++) {
            out.print("" + Math.floor(V.get(i, j) * 100) / 100);
            if (j != m - 1) {
               out.print(", ");
            }
         }
         out.print("}");
         if (i == n - 1) {
            out.print("}");
         }
         else if (i != n - 1) {
            out.print(", ");
         }
      }
   }
   
}
